package net.orange.architecture.audit;

import java.lang.reflect.Method;

import net.orange.framework.model.User;

/**
 * The event action message.
 */
public class ActionInfo implements IAuditInfo {

	/**
	 * The event method
	 */
	private Method method;

	/**
	 * The method parameters
	 */
	private Object[] parameters;

	/**
	 * The method return value
	 */
	private Object returns;

	/**
	 * The current user to perform this event.
	 */
	private User user;

	/**
	 * Constructor with the method, parameter, return value and current user.
	 * 
	 * @param method
	 *            The method name.
	 * @param parameters
	 *            The parameters.
	 * @param returns
	 *            The return object.
	 * @param user
	 *            The current user
	 */
	public ActionInfo(Method method, Object[] parameters, Object returns,
			User user) {
		this.method = method;
		this.parameters = parameters;
		this.returns = returns;
		this.user = user;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.audit.IAuditInfo#toInfo()
	 */
	public String toInfo() {
		try {
			StringBuffer s = new StringBuffer();
			// Write down the user.
			if (user != null) {
				s.append("  [" + user.getUser_name() + "]");
			} else {
				s.append("  [Visitor]");
			}

			// Write the action class & method.
			s.append(" " + getClassName(method.getDeclaringClass().getName())
					+ "@" + method.getName() + "(");

			// Write the action parameters
			if (parameters != null && parameters.length > 0) {
				for (int i = 0; i < parameters.length; ++i) {
					s.append(String.valueOf(parameters[i])
							.replaceAll("\n", " "));
					if (i < parameters.length - 1)
						s.append(",");
				}
			}
			s.append(")");

			return s.toString();
		} catch (Exception e) {
			System.out.println("Cann't write down the user action");
			return "";
		}
	}

	/**
	 * @return Returns the method.
	 */
	public Method getMethod() {
		return method;
	}

	/**
	 * @param method
	 *            The method to set.
	 */
	public void setMethod(Method method) {
		this.method = method;
	}

	/**
	 * @return Returns the parameters.
	 */
	public Object[] getParameters() {
		return parameters;
	}

	/**
	 * @param parameters
	 *            The parameters to set.
	 */
	public void setParameters(Object[] parameters) {
		this.parameters = parameters;
	}

	/**
	 * @return Returns the returns.
	 */
	public Object getReturns() {
		return returns;
	}

	/**
	 * @param returns
	 *            The returns to set.
	 */
	public void setReturns(Object returns) {
		this.returns = returns;
	}

	/**
	 * @return Returns the user.
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            The user to set.
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * Get the simple class name.
	 * 
	 * @param s
	 *            full The class name include the package.
	 * @return The simple class name.
	 * @throws Exception
	 */
	private String getClassName(String s) throws Exception {
		if (s == null)
			return "";
		return s.substring(s.lastIndexOf(".") + 1, s.length());
	}
}
